package com.example.bai5;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.EditText;
import com.example.bai5.object.*;
public class MainActivity extends Activity {

	ArrayList<Catalog> arrspinner;
	ArrayAdapter<Catalog> spinadapter;
	
	ArrayList<Product> arrlist;
	ArrayAdapter<Product> listadapter;
	
	EditText edtid;
	EditText edtnm;
	Button btn;
	Spinner spin;
	ListView list;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getControls();
        initControls();
        addEventForControls();
}

    private void getControls()
    {
    	edtid=(EditText) findViewById(R.id.editId);
    	edtnm=(EditText) findViewById(R.id.editNm);
    	spin=(Spinner) findViewById(R.id.spinner1);
    	list=(ListView) findViewById(R.id.list);
    	btn= (Button) findViewById(R.id.btnInput);
    	
    	arrspinner= new ArrayList<Catalog>();
    	
    	spinadapter= new ArrayAdapter<Catalog>(this, android.R.layout.simple_spinner_item,arrspinner);
    
    	spin.setAdapter(spinadapter);
    	arrlist= new ArrayList<Product>();
    	listadapter= new ArrayAdapter<Product>(this,android.R.layout.simple_list_item_1,arrlist);
    	list.setAdapter(listadapter);
    }
    
    private void initControls()
    {
    	Catalog cat1=new Catalog("1", "SamSung");
    	 Catalog cat2=new Catalog("2", "Iphone");
    	 Catalog cat3=new Catalog("3", "IPad");
    	 arrspinner.add(cat1);
    	 arrspinner.add(cat2);
    	 arrspinner.add(cat3);
    	 spinadapter.notifyDataSetChanged();
    }
    private void addEventForControls()
    {
    	btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Product p= new Product();
				p.setId(edtid.getText().toString());
				p.setName(edtnm.getText().toString());
				
				Catalog c= (Catalog) spin.getSelectedItem();
				p.setDMuc(c);
				c.addProduct(p);
				
				reloadList(c);
			}
		});
    	spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				reloadList(arrspinner.get(arg2));
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
    }
    
    private void reloadList(Catalog c)
    {
    	arrlist.clear();
    	arrlist.addAll(c.getListProduct());
    	
    	listadapter.notifyDataSetChanged();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
